package com.skyable.common.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GaoYuan
 * @date 2020/3/18 13:22
 * @desc
 */
public class ListUtil {

    /**
     * 将List<Object> 转为 List<String>
     * @param objectList
     * @return
     */
    public static List<String> listObjToListStr(List<?> objectList) {
        List<String> strList = new ArrayList<>();
        if(objectList != null && !objectList.isEmpty()) {
            for(Object obj : objectList) {
                strList.add(JSONObject.toJSONString(obj));
            }
        }
        return strList;
    }

    /**
     * 将List<String> 转为 List<T>
     * @param strList
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T>List<T> listStrToListObj(List<String> strList,Class<T> clazz) {
        List<T> objList = new ArrayList<>();
        if(strList != null && !strList.isEmpty()) {
            for(String str : strList) {
                objList.add(JSONObject.parseObject(str,clazz));
            }
        }
        return objList;
    }

}
